<?php

function secure_db_text($txt){
	return mysql_real_escape_string($txt);
}

function from_db_text($txt){
	return stripslashes($txt);
}

function construct_insert_fields($data){
	if( is_array($data) ){
		
		$total=count($data);
		$camp='';
		$value='';
		$m=0;
		foreach ($data as $key => $val){

			$camp.=$key;
			if(($total-1)!=$m){
				$camp.=", ";
			}
			$value.="'".secure_db_text($val)."'";
			if(($total-1)!=$m){
				$value.=", ";
			}
			$m++;
		}
		
		return "($camp) VALUES ($value)";
	}else if( !is_array($data) && !is_array($fields) ){
		return "($data) VALUES ($fields)";
	}
}

function construct_update_fields($data,$validFields){
	if( is_array($data) ){
		
		$total=count($data);
		$camp='';
		$value='';
		foreach ($data as $key => $val){
			if(in_array( $key, $validFields )){
				$value.="$key = '".secure_db_text($val)."'";
				$value.=", ";
			}
		}
		
		return substr($value ,0, -2);
	}
}


?>